<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <h3>登录</h3>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="用户类型" prop="staff_status">
        <el-radio-group v-model="ruleForm.staff_status">
          <el-radio :label="'1'">员工</el-radio>
          <el-radio :label="'2'">经理</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { submitStaff } from "../api/api";

export default {
  // 定义属性
  name: "Login",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入用户名"));
      }
      console.log(value.length);
      setTimeout(() => {
        if (value.length < 2 || value.length > 6) {
          callback(new Error("请输入2-6个字符"));
        } else {
          callback();
        }
      }, 100);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (value.length >= 4 && value.length <= 18) {
          callback();
        } else {
          callback(new Error("请输入6-18位密码"));
        }
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        userName: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        userName: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 请求登录接口
          submitStaff({
            staff_name: this.ruleForm.userName,
            staff_pass: this.ruleForm.pass,
          }).then((res) => {
            console.log(res);
            if (res.code == 200) {
              localStorage.setItem(
                "communityUser",
                JSON.stringify(this.ruleForm)
              );
              this.$router.push({ path: "/index" });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
};
</script>

<style lang='scss' scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(../assets/img/dlback2.jpg) no-repeat 100% 100%;
  background-size: cover;
  .el-form {
    width: 400px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border: 1px solid #ccc;
    border-radius: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px #ccc;
    padding: 30px;
    box-sizing: border-box;
    h3 {
      text-align: center;
      margin-bottom: 20px;
      color: #333;
    }
  }
}
</style>